/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RoomPanel.java
 *
 * Created on 15 Feb 2011, 3:53:17 PM
 */
package za.co.pas.spaceandtime.view.panel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import za.co.pas.business.constant.Constant;
import za.co.pas.spaceandtime.data.Bed;
import za.co.pas.spaceandtime.data.Room;
import za.co.pas.spaceandtime.model.combo.RoomTypeComboModel;
import za.co.pas.spaceandtime.model.list.BedListModel;
import za.co.pas.spaceandtime.view.dlg.BedDialog;

/**
 *
 * @author alabuschagne
 */
public class RoomPanel extends JPanel
{

    private Room currentRoom = new Room();
    private BedListModel bedListModel = new BedListModel();
    private boolean dirty = false;
    private RoomTypeComboModel roomTypeComboModel = new RoomTypeComboModel();

    /** Creates new form RoomPanel */
    public RoomPanel()
    {
        initComponents();        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        companyNamePanel = new javax.swing.JPanel();
        roomNameTextField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        roomDescriptionTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        shareBedsCheckBox = new javax.swing.JCheckBox();
        macCapSpinner = new javax.swing.JSpinner();
        jLabel3 = new javax.swing.JLabel();
        enabledCheckBox = new javax.swing.JCheckBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        roomTypeComboBox = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        bedsList = new javax.swing.JList();

        companyNamePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "Name", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N

        roomNameTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                roomNameTextFieldKeyPressed(evt);
            }
        });

        jLabel1.setLabelFor(roomNameTextField);
        jLabel1.setText("Room name / number:");

        roomDescriptionTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                roomDescriptionTextFieldKeyPressed(evt);
            }
        });

        jLabel2.setLabelFor(roomDescriptionTextField);
        jLabel2.setText("Description:");

        shareBedsCheckBox.setToolTipText("Like a hostel");
        shareBedsCheckBox.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);

        macCapSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(1), Integer.valueOf(1), null, Integer.valueOf(1)));

        jLabel3.setLabelFor(macCapSpinner);
        jLabel3.setText("Max capacity:");

        enabledCheckBox.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);

        jLabel4.setLabelFor(enabledCheckBox);
        jLabel4.setText("Enabled:");

        jLabel5.setLabelFor(shareBedsCheckBox);
        jLabel5.setText("Room shared:");

        roomTypeComboBox.setModel(roomTypeComboModel);

        jLabel6.setText("Room type:");

        javax.swing.GroupLayout companyNamePanelLayout = new javax.swing.GroupLayout(companyNamePanel);
        companyNamePanel.setLayout(companyNamePanelLayout);
        companyNamePanelLayout.setHorizontalGroup(
            companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(companyNamePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(companyNamePanelLayout.createSequentialGroup()
                        .addComponent(shareBedsCheckBox)
                        .addContainerGap())
                    .addComponent(enabledCheckBox)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, companyNamePanelLayout.createSequentialGroup()
                        .addGroup(companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(roomTypeComboBox, javax.swing.GroupLayout.Alignment.LEADING, 0, 234, Short.MAX_VALUE)
                            .addComponent(roomDescriptionTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)
                            .addComponent(roomNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)
                            .addComponent(macCapSpinner, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())))
        );
        companyNamePanelLayout.setVerticalGroup(
            companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(companyNamePanelLayout.createSequentialGroup()
                .addGroup(companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(roomNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(roomDescriptionTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(macCapSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel5)
                    .addComponent(shareBedsCheckBox))
                .addGap(3, 3, 3)
                .addGroup(companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel4)
                    .addComponent(enabledCheckBox))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(roomTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(12, 12, 12))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "Beds", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N
        jPanel1.setLayout(new java.awt.BorderLayout());

        bedsList.setModel(bedListModel);
        bedsList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                bedsListMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(bedsList);

        jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(companyNamePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(companyNamePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void roomNameTextFieldKeyPressed(java.awt.event.KeyEvent evt)//GEN-FIRST:event_roomNameTextFieldKeyPressed
    {//GEN-HEADEREND:event_roomNameTextFieldKeyPressed
        setDirty(true);
}//GEN-LAST:event_roomNameTextFieldKeyPressed

    private void roomDescriptionTextFieldKeyPressed(java.awt.event.KeyEvent evt)//GEN-FIRST:event_roomDescriptionTextFieldKeyPressed
    {//GEN-HEADEREND:event_roomDescriptionTextFieldKeyPressed
        setDirty(true);
}//GEN-LAST:event_roomDescriptionTextFieldKeyPressed

    private void bedsListMouseReleased(java.awt.event.MouseEvent evt)//GEN-FIRST:event_bedsListMouseReleased
    {//GEN-HEADEREND:event_bedsListMouseReleased
        if (evt.isPopupTrigger())
        {
            JPopupMenu Pmenu = new JPopupMenu();
            JMenuItem menuItem = new JMenuItem("New");
            Pmenu.add(menuItem);
            menuItem.addActionListener(new ActionListener()
            {

                @Override
                public void actionPerformed(ActionEvent e)
                {
                    editBedDetails(null);
                }
            });
            int k = bedsList.getSelectedIndex();
            if (k != -1)
            {
                menuItem = new JMenuItem("Edit");
                Pmenu.add(menuItem);
                menuItem.addActionListener(new ActionListener()
                {

                    @Override
                    public void actionPerformed(ActionEvent e)
                    {
                        int k = bedsList.getSelectedIndex();
                        if (k != -1)
                        {
                            Bed bed = (Bed) bedListModel.getElementAt(k);
                            editBedDetails(bed);
                        }
                    }
                });
            }
            Pmenu.show(evt.getComponent(), evt.getX(), evt.getY());
        }
}//GEN-LAST:event_bedsListMouseReleased
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList bedsList;
    private javax.swing.JPanel companyNamePanel;
    private javax.swing.JCheckBox enabledCheckBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSpinner macCapSpinner;
    private javax.swing.JTextField roomDescriptionTextField;
    private javax.swing.JTextField roomNameTextField;
    private javax.swing.JComboBox roomTypeComboBox;
    private javax.swing.JCheckBox shareBedsCheckBox;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the dirty
     */
    public boolean isDirty()
    {
        return dirty;
    }

    /**
     * @param dirty the dirty to set
     */
    public void setDirty(boolean dirty)
    {
        this.dirty = dirty;
    }

    private void editBedDetails(Bed bed)
    {
        BedDialog bd = new BedDialog(Constant.getParentFrame(this), true);
        if (bed != null)
        {
            bd.setBed(bed);
        }
        bd.setVisible(true);
        if (bd.isOk())
        {
            if (bed == null)
            {
                bed = bd.getBed();
                this.currentRoom.add(bed);
            }
            else
            {
                Bed b = bd.getBed();
                bed.setBedType(b.getBedType());
                bed.setEnabled(b.getEnabled());
                bed.setMaxCapacity(b.getMaxCapacity());
            }
        }
    }

    public void setRoom(Room room)
    {
        if (room != null)
        {
            currentRoom = room.clone();
        }
        else
        {
            currentRoom = new Room();
        }
        updateFields();
    }

    public Room getRoom()
    {
        updateData();
        return currentRoom;
    }

    private void updateData()
    {
        if (currentRoom == null)
        {
            currentRoom = new Room();
        }
        currentRoom.setName(this.roomNameTextField.getText().trim());
        currentRoom.setDescription(this.roomDescriptionTextField.getText().trim());
        currentRoom.setEnabled(this.enabledCheckBox.isSelected());
        currentRoom.setMaxCapacity((Integer) this.macCapSpinner.getValue());
        currentRoom.setShareBeds(this.shareBedsCheckBox.isSelected());
    }

    private void updateFields()
    {
        this.roomNameTextField.setText(currentRoom.getName());
        this.roomDescriptionTextField.setText(currentRoom.getDescription());
        bedListModel.setBeds(currentRoom.getBeds());
        this.shareBedsCheckBox.setSelected(currentRoom.getShareBeds());
        this.macCapSpinner.setValue(currentRoom.getMaxCapacity() == null ? new Integer(1) : currentRoom.getMaxCapacity());
        this.enabledCheckBox.setSelected(currentRoom.getEnabled());
    }
}
